"""用于管理和启动相关的配置"""
import logging
from flask_migrate import MigrateCommand, Migrate
from flask_script import Manager
from info import config_init, db, models
from info.models import User

app = config_init('development')
# 给文件添加命令行模式
manager = Manager(app)
# 添加数据库的迁移命令
# 使应用和数据库关联
Migrate(app, db)
# 给manager添加命令
manager.add_command('db', MigrateCommand)


@manager.option("-n", "-name", dest="name")
@manager.option("-p", "-password", dest="password")
def admin(name, password):
    if not all([name, password]):
        return
    use = User()
    use.nick_name = name
    use.password = password
    use.is_admin = True
    use.mobile = name

    try:
        db.session.add(use)
        db.session.commit()
        print("创建成功")
    except Exception as e:
        db.session.rollback()
        logging.error(e)


if __name__ == '__main__':
    # print(app.url_map)
    manager.run()
